/**
 * Created by ybjaa on 2017/4/20.
 */
import {Component, Inject, Injectable, Injector, OnInit} from '@angular/core';
import {LogOutService} from "../users/logout/logout.service";
import {ActivatedRoute, Router} from "@angular/router";
import {MenusService} from "../menus/menus.service";
import {Menu} from "../model/Menu";
import {ArrayModel} from "../model/ArrayModel";
import {BaseComponent} from "../base.component";
import {AnnouncementService} from "../announcement/announcement.service";
import {PersonService} from "../person/persion.service";
import {ArticleWrite} from "../article/write/article.write.component";
import {ArticleManage} from "../article/article.manage.component";
import {ArticleService} from "../article/article.service";

@Component({
    selector: 'my-app',
    templateUrl:"./manage.component.html",
    styleUrls:["./manage.component.css"],
    providers:[
        LogOutService,
        MenusService,
        AnnouncementService,
        PersonService,
        ArticleService
    ]
})
export class ManageComponent extends BaseComponent implements OnInit {

    selectMenu:Menu;

    secendSelectMenu:Menu;

    _topMenus:ArrayModel<Menu>=new ArrayModel(Menu);

    get topMenus():Menu[]
    {
        return this._topMenus.Datasource;
    }

    _secendMenus:Menu[];

    get secendMenus():Menu[]
    {
        return this._secendMenus;
    }

    ngOnInit(): void {
        this.getTopMenus();
    }


    constructor(private logout:LogOutService,
                private menuService:MenusService,
                private route:ActivatedRoute,
                private router:Router){super()};


    /**
     * 获取二级菜单
     * @param menu
     */
    public getSecendMenus(menu:Menu):void{
        this.selectMenu=menu;

        this._secendMenus=menu.children;

    }


    /**
     * 获得当前用户权限内所属的菜单
     */
    public getTopMenus():void{

        if(this.menuService._customMenus)
        {
            this._topMenus=this.menuService._customMenus;
            return;
        }

         this.menuService.getCustomMenus()
            .then(response=>{

                if(!response.result){
                    this.setMessage(null,response.message,true);
                    return;
                }

                this._topMenus.Datasource=response.data;
                this.menuService._customMenus=this._topMenus;

                if(this.topMenus.length>0)
                {
                    this.getSecendMenus(this.topMenus[0]);
                }
            })
            .catch(ex=>{
                this._topMenus.Datasource=[];
                this.setMessage(ex,null,true);
            })
    }


    public setSecendMenu(menu:Menu):void{
        this.secendSelectMenu=menu;
        if(menu.MenuURL)
        {

            this.router.navigate([menu.MenuURL],{ relativeTo: this.route });
        }
    }



}
